﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BO;
using UIControls;

using BrightIdeasSoftware;
using System.Diagnostics;
using System.Collections;
using System.Data.Entity;

using UI.Core;
namespace UI
{
    public partial class StaffListEditForm : EditFormBase, IMenuForm
    {
        public StaffListEditForm()
        {
            InitializeComponent();
      
            MyGrid = grid;

        }

        StaffList mySource;

        protected DateTime BeginDate
        {
            get { return dateBegin.DateTimePicker.Value.Date; }
        }
        protected DateTime EndDate
        {
            get { return dateEnd.DateTimePicker.Value.Date; }
        }
        protected string companyName
        {
            get
            {
                if (cbCompany.ComboBox.SelectedValue == null)
                    return string.Empty;
                return cbCompany.ComboBox.SelectedValue.ToString();
            }
        }
        protected string centerName
        {
            get
            {
                if (cbCenter.ComboBox.SelectedValue == null)
                    return string.Empty;
                return cbCenter.ComboBox.SelectedValue.ToString();
            }
        }
        protected string departmentName
        {
            get
            {
                if (cbDepartment.ComboBox.SelectedValue == null)
                    return string.Empty;
                return cbDepartment.ComboBox.SelectedValue.ToString();
            }
        }
       

        protected override void OnQuery()
        {
            BindFileds();
            FetchData();
            DataBind();
        }

        protected void FildChanged(object sender, EventArgs e)
        {
            base.FildChanged(sender, e);
        }

        void BindFileds()
        {
            var d1 = ListFactory<CompanyList>.GetList("")
               .Select(item => item.Name).Distinct().ToList();
            d1.Insert(0, "");
            cbCompany.ComboBox.DataSource = d1;


            var d2 = ListFactory<DepartmentList>.GetList("").Select(item => item.Name).Distinct().ToList();
            d2.Insert(0, "");
            cbDepartment.ComboBox.DataSource = d2;


            var d5 = ListFactory<CenterList>.GetList("").Select(item => item.Name).Distinct().ToList();
            d5.Insert(0, "");
            cbCenter.ComboBox.DataSource = d5;
        }

        private void Form_Load(object sender, EventArgs e)
        {
            var d = DateTime.Now;
            dateBegin.DateTimePicker.Value = new DateTime(d.Year, d.Month, 1);
            dateEnd.DateTimePicker.Value = d;

        
            OnSetGridColumns();
            InitGrid();
            OnQuery();

            this.dateBegin.DateTimePicker.ValueChanged += new System.EventHandler(this.FildChanged);
            this.dateBegin.DateTimePicker.ValueChanged += new System.EventHandler(this.FildChanged);
            this.dateEnd.DateTimePicker.ValueChanged += new System.EventHandler(this.FildChanged);
            this.cbDepartment.ComboBox.SelectedValueChanged += new System.EventHandler(this.FildChanged);
            this.cbCompany.ComboBox.SelectedValueChanged += new System.EventHandler(this.FildChanged);
            this.cbCenter.ComboBox.SelectedValueChanged += new System.EventHandler(this.FildChanged);
         
        }
      
        protected override void FetchData()
        {
            mySource = ListFactory<StaffList>.GetList(
                string.Format(@"
CompanyName Like '%{0}%'
And CenterName Like '%{1}%'
And DepartmentName Like '%{2}%'
And {3}
", companyName, centerName, departmentName,GlobalVar.User.CompanyFilter), "EntryDate desc,ExitDate desc", 0
                );
                //(from item in mySource.Local
                //        where item.CompanyName.Contains(companyName)
                //           && item.CenterName.Contains(centerName)
                //        && item.DepartmentName.Contains(departmentName)
                       
                //        orderby item.EntryDate descending, item.ExitDate descending
                //        select item
                //           ).ToList();

          //  mySource = mySource.Local;

            GridDataSource = mySource;

            MyGrid.RowObjectType = typeof(Staff);
        }
        protected override void DataBind()
        {
            base.DataBind();
          
            RowCount = MyGrid.Items.Count;
        }

        void OnSetGridColumns()
        {
            var cols = new List<OLVColumn>();
           
            cols.Add(new OLVColumn("编号", "ID") { IsEditable = false, Width = 50 });
            cols.Add(new OLVColumn(FieldHelper.cnName.Staff, "Name") { Width = 60 });
            cols.Add(new OLVColumn("生日", "BirthDay")
            {
                Width = 110,
                AspectToStringFormat = "{0:d}",
                ClusteringStrategy = new DateTimeClusteringStrategy(DateTimePortion.Day | DateTimePortion.Month | DateTimePortion.Year, "yyyy-MM-dd"),
            });
            cols.Add(new OLVColumn("性别", "Sex")
            {
                Width = 60,
                AspectToStringConverter =
                    new AspectToStringConverterDelegate(item => (bool)item == true ? "男" : "女")
            });
            cols.Add(new OLVColumn("挑战目标", "TargetAmount") { Width = 100 });
            var cDate = new OLVColumn(FieldHelper.cnName.EntryDate, "EntryDate")
            {
                Width = 110,
                AspectToStringFormat = "{0:d}",
                 ClusteringStrategy = new DateTimeClusteringStrategy(DateTimePortion.Day| DateTimePortion.Month| DateTimePortion.Year, "yyyy-MM-dd"),
              

            };

            var cDate1 = new OLVColumn(FieldHelper.cnName.ExitDate, "ExitDate")
            {
                Width = 110,
                AspectToStringFormat = "{0:d}",
                ClusteringStrategy = new DateTimeClusteringStrategy(DateTimePortion.Day | DateTimePortion.Month | DateTimePortion.Year, "yyyy-MM-dd"),

                AspectToStringConverter =
                new AspectToStringConverterDelegate(item => ((DateTime)item).Date == Staff.DefaultExitDate ? "" : ((DateTime)item).ToString("yyyy-MM-dd")),
            };
         
            cols.Add(cDate);
            cols.Add(cDate1);
            cols.Add(new OLVColumn(FieldHelper.cnName.Company, "CompanyName") { Width = 60,
            IsEditable=GlobalVar.User.AllowAllCompany
            });
            cols.Add(new OLVColumn(FieldHelper.cnName.Center, "CenterName") { Width = 60 });
            cols.Add(new OLVColumn(FieldHelper.cnName.Department, "DepartmentName") { Width = 60 });



            cols.Add(new OLVColumn(FieldHelper.cnName.IsNormal, "IsNormal")
            {
               
                Width = 60,
                 AspectToStringConverter =
                    new AspectToStringConverterDelegate(item => (bool)item == true ? "是" : "否")
            }
             );
            cols.Add(new OLVColumn("年会", "IsYearMeeting")
            {

                Width = 60,
                AspectToStringConverter =
                   new AspectToStringConverterDelegate(item => (bool)item == true ? "是" : "否")
            }
         );
            cols.Add(new OLVColumn("职级", "StaffLevel") { Width = 50, FillsFreeSpace = true });
            cols.Add(new OLVColumn(FieldHelper.cnName.Remark, "Remark") {Width=50, FillsFreeSpace = true });




            MyGrid.AllColumns.AddRange(cols);


            MyGrid.Columns.AddRange(cols.ToArray());

        }
        protected override void OnGridFilterd(UIControls.Grid.OSGrid.FilterdEventArgs e)
        {
           
        }

    

        protected override object OnAdd()
        {

            var obj = new Staff();
            if (GlobalVar.User.AllowAllCompany)
                obj.CompanyName = companyName;
            else
                obj.CompanyName = GlobalVar.User.CompanyName;
            obj.DepartmentName = departmentName;
            obj.CenterName = centerName;

            obj.EntryDate = EndDate;
            obj.ExitDate = Staff.DefaultExitDate;
            return obj;


        }


        protected override void OnSave()
        {
            try
            {
                foreach (Staff delObj in RemovedList)
                {
                    if (delObj == null)
                        continue;
                    if (delObj.ID == 0)
                        continue;
                    mySource.Remove(delObj);
                }
                RemovedList.Clear();

                foreach (Staff obj in MyGrid.Objects)
                {
                    if (obj == null)
                        continue;
                    if (obj.ID == 0)
                        mySource.Add(obj);

                }

                mySource.Save();
                OnQuery();
            }
            catch (ExceptionEx exp)
            {
                ExcptionHandler.ShowExcption(exp);
            }

        }


    }
}
